package com.igeek.dao;

import com.igeek.domain.Order;
import com.igeek.domain.OrderItem;
import com.igeek.domain.Product;
import com.igeek.utils.JDBCTools;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class Orderdao {
    QueryRunner qr =new QueryRunner();
    QueryRunner queryRunner=new QueryRunner(JDBCTools.getDataSource());
    String sql="";
    public void save(Order order) throws SQLException {
        sql="insert into orders values(?,now(),?,0,'','','',?)";
        qr.update(JDBCTools.local.get(),sql,order.getOid(),order.getTotal(),order.getUser().getUid());
    }

    public void saveorder(OrderItem item) throws SQLException {
        sql="insert into orderitem values(?,?,?,?,?)";
        qr.update(JDBCTools.local.get(),sql,item.getItemid(),item.getCount(),
                item.getSubtotal(),item.getProduct().getPid(),item.getOrder().getOid());
    }

    public void update(Order order) {
        sql = "update orders set address = ?,name=?,telephone =? where oid = ?" ;
        try {
            queryRunner.update(sql,order.getAddress(),order.getName(),order.getTelephone(),order.getOid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<Order> findAllorder(String uid) {
        sql="select * from orders where uid=?";
        try {
            return queryRunner.query(sql,new BeanListHandler<>(Order.class),uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<OrderItem> findAllitems(String oid) {
        sql="select * from orderitem where oid=?";
        try {
            return queryRunner.query(sql,new BeanListHandler<>(OrderItem.class),oid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Product findproduct(String itemid) {
        sql="select p.* from product p,orderitem o where itemid=? and p.pid=o.pid";
        try {
            return queryRunner.query(sql,new BeanHandler<>(Product.class),itemid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Order> getPageData(int page, int i) {
        sql="select * from order limit ?,?";
        int offset=(page-1)*i;
        try {
            return qr.query(sql,new BeanListHandler<>(Order.class),offset,i);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public int getCount() {
        sql="select count(*) from order";
        try {
            return  Integer.parseInt(qr.query(sql,new ScalarHandler<Long>()).toString()) ;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

}
